/*
 * Copyright 2012 Alain Pannetrat (L1L1@gmx.com)
 * 
 * This file is part of cookie-miner.
 * 
 * cookie-miner is free software: you can redistribute it and/or modify 
 * it under the terms of the GNU General Public License as published by 
 * the Free Software Foundation, either version 3 of the License, or 
 * (at your option) any later version.
 * 
 * cookie-miner is distributed in the hope that it will be useful, but 
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License 
 * along with cookie-miner. If not, see http://www.gnu.org/licenses/.
 *
 * @version: $Id$
 *
 */
#include "vectors.h"
#include <stdlib.h>
#include <stdio.h>
#include <vector>

#ifdef DO_SIMD
/* STATIC */

void *malloc16a(size_t sz)
{
	unsigned long *m16 = (unsigned long *)malloc(sz+15+sizeof(void*));
	unsigned long *ptr = (unsigned long *)(((unsigned long)m16+sizeof(void*)+15)&(~0xF));
	*(ptr-1)=(unsigned long)m16;
	return ptr;
}

void free16a(void *ptr)
{
 	unsigned long *m16 = (unsigned long *)(*(((unsigned long *)ptr)-1));
       	free(m16);	
}

//std::vector<vvector*> vector_pool;
free_list_t* vector_free = NULL;

#define VBLOCK_SIZE 1024
vvector_t *Vector::alloc_vector()
{
	vvector_t *res;

	if (vector_free==NULL)
	{
		vvector_t *block = (vvector_t*)malloc16a(VBLOCK_SIZE*sizeof(vvector_t));
	        for (unsigned i = VBLOCK_SIZE;i>0;i--)
		{
			block[i-1].f.next = vector_free;
			vector_free = &(block[i-1].f);
		}
	}

	res = (vvector_t *)vector_free;
        vector_free = vector_free->next;	

	return res;
}

void Vector::free_vector(vvector_t *vec)
{
	free_list_t *free_vec = (free_list_t *)vec;

	free_vec->next = vector_free;
	vector_free = free_vec;
}
#endif
/**********/

const Vector Vector::null = Vector(0);

bool Vector::from_string(const char *str)
{	
	return sscanf(str,"%f,%f,%f",k,k+1,k+2)==3;
}	



